#!/bin/bash

# dreamy-trac - Automatically install and manage Trac on DreamHost
# Author: Petar Maric <http://www.petarmaric.com>
# Licence: BSD
#
# DON'T CHANGE THIS FILE UNLESS YOU KNOW WHAT YOU'RE DOING!

# Abort on any error
set -e

# Version of Python used for installing dreamy-trac 
PYTHON_VERSION=PYTHON_VERSION_TEMPLATE

# Root directory for all user-specific libraries and programs
PKG_DIR=PKG_DIR_TEMPLATE

# Root directory for all Trac projects
TRAC_PROJECTS_DIR=TRAC_PROJECTS_DIR_TEMPLATE

# Root directory with all templates used by this script
TEMPLATES_DIR=${PKG_DIR}/share/dreamy-trac/templates


create_trac_env() {
    ${TRAC_ADMIN_COMMAND} initenv "${TRAC_PROJECT_NAME}" \
     sqlite:db/${TRAC_PROJECT_ID}.sqlite svn ${SVN_PROJECT_DIR}
}

create_superuser() {
    echo
    echo "Enter password for the superuser (you have only one try): "    
    htpasswd -c ${TRAC_PROJECT_DIRECTORY}/${TRAC_PROJECT_ID}.passwd ${USERNAME}

    ${TRAC_ADMIN_COMMAND} permission add admins TRAC_ADMIN
    ${TRAC_ADMIN_COMMAND} permission add ${USERNAME} admins
}

modify_trac_ini() {
    echo "

##############################################################################
#
# Added by dreamy-trac
#    
[components]
webadmin.* = enabled
trac.web.auth.LoginModule = disabled
acct_mgr.* = enabled

[account-manager]
password_format = htpasswd
password_file = ${TRAC_PROJECT_DIRECTORY}/${TRAC_PROJECT_ID}.passwd

[trac]
htdocs_location = /chrome/common/
##############################################################################

    " >> ${TRAC_PROJECT_DIRECTORY}/conf/trac.ini
}

add_post_commit_hook() {
    cp -f ${TEMPLATES_DIR}/post-commit ${SVN_PROJECT_DIR}/hooks
    POST_COMMIT_HOOK_FILE=${SVN_PROJECT_DIR}/hooks/post-commit
    perl -pi -e "s|PYTHON_VERSION_TEMPLATE|${PYTHON_VERSION}|gi" ${POST_COMMIT_HOOK_FILE}
    perl -pi -e "s|PKG_DIR_TEMPLATE|${PKG_DIR}|gi" ${POST_COMMIT_HOOK_FILE}
    perl -pi -e "s|TRAC_PROJECT_DIR_TEMPLATE|${TRAC_PROJECT_DIRECTORY}|gi" ${POST_COMMIT_HOOK_FILE}
    perl -pi -e "s|USER_TEMPLATE|${USER}|gi" ${POST_COMMIT_HOOK_FILE}
    chmod 755 ${POST_COMMIT_HOOK_FILE}
}

setup_trac_env() {
    create_trac_env
    create_superuser
    modify_trac_ini
    add_post_commit_hook
}

setup_fcgi() {
    cp -f ${TEMPLATES_DIR}/index.fcgi ${TRAC_SITE_PATH}
    perl -pi -e "s|PYTHON_VERSION_TEMPLATE|${PYTHON_VERSION}|gi" ${TRAC_SITE_PATH}/index.fcgi
    perl -pi -e "s|PKG_DIR_TEMPLATE|${PKG_DIR}|gi" ${TRAC_SITE_PATH}/index.fcgi
    perl -pi -e "s|TRAC_PROJECT_DIR_TEMPLATE|${TRAC_PROJECT_DIRECTORY}|gi" ${TRAC_SITE_PATH}/index.fcgi
    perl -pi -e "s|USER_TEMPLATE|${USER}|gi" ${TRAC_SITE_PATH}/index.fcgi
    chmod 755 ${TRAC_SITE_PATH}/index.fcgi
}

setup_pretty_urls() {
    mkdir -p ${TRAC_SITE_PATH}/chrome
    ln -sf ${PKG_DIR}/share/trac/htdocs/ ${TRAC_SITE_PATH}/chrome/common
}

setup_htaccess() {
    cp -f ${TEMPLATES_DIR}/.htaccess ${TRAC_SITE_PATH}
    chmod 644 ${TRAC_SITE_PATH}/.htaccess
}

make_trac_web_accessible() {
    setup_fcgi
    setup_pretty_urls
    setup_htaccess
}



echo "Before continuing you must satisfy these preconditions:"
echo "   * working subversion repository (on *this* shell account),"
echo "   * working domain name (on this shell account) and"
echo "   * enabled FastCGI for selected domain name."
echo 
echo -n "Create a new Trac project? (y/n) "
read verify
if [ "$verify" == "y" ]; then
    verify="n"
    echo
    while [ "$verify" != y ];
    do
	echo -n "Enter subversion project ID (e.g. svn_example_project): "
	read SVN_PROJECT_ID
	echo -n "You entered ${SVN_PROJECT_ID}. Is this correct? (y/n) "
	read verify
    done
    
    verify="n"
    echo
    while [ "$verify" != y ];
    do
	echo -n "Enter Trac project ID (e.g. trac_example_project): "
	read TRAC_PROJECT_ID
	echo -n "You entered ${TRAC_PROJECT_ID}. Is this correct? (y/n) "
	read verify
    done
    
    verify="n"
    echo
    while [ "$verify" != y ];
    do
	echo -n "Enter Trac project name (e.g. Trac example project): "
	read TRAC_PROJECT_NAME
	echo -n "You entered ${TRAC_PROJECT_NAME}. Is this correct? (y/n) "
	read verify
    done
    
    verify="n"
    echo
    while [ "$verify" != y ];
    do
        echo -n "Enter the *absolute* path to Trac domain name (e.g. /home/user/sites/trac.example.org): "
        read TRAC_SITE_PATH
        echo -n "You entered ${TRAC_SITE_PATH}. Is this correct? (y/n) "
        read verify
    done
    
    verify="n"
    echo
    while [ "$verify" != y ];
    do
        echo -n "Enter username for the superuser (e.g. admin): "
        read USERNAME
        echo -n "You entered ${USERNAME}. Is this correct? (y/n) "
        read verify
    done

    
    SVN_PROJECT_DIR=${HOME}/svn/${SVN_PROJECT_ID}
    TRAC_PROJECT_DIRECTORY=${TRAC_PROJECTS_DIR}/${TRAC_PROJECT_ID}
    TRAC_ADMIN_COMMAND="trac-admin ${TRAC_PROJECT_DIRECTORY}"
    
    
    setup_trac_env
    make_trac_web_accessible
fi

